//
//  UIButton+ZJCategory.m
//  Button_链式
//
//  Created by zhengju on 2017/4/16.
//  Copyright © 2017年 郑俱. All rights reserved.
//

#import "UIButton+ZJCategory.h"

#import <objc/runtime.h>

static const char *overviewKey = "buttonActionKey";

@implementation UIButton (ZJCategory)

#pragma mark - UIView
-(UIButton* (^) (CGRect))home_frame
{
    return ^(CGRect frame){
        self.frame = frame;
        return self;
    };
}

-(UIButton* (^) (CGRect))home_bounds
{
    return ^(CGRect bounds){
        self.bounds = bounds;
        return self;
    };
}

-(UIButton *(^)(UIColor *))home_backgroundColor
{
    return ^(UIColor *backgroundColor){
        self.backgroundColor = backgroundColor;
        return self;
    };
}

-(UIButton *(^)(CGPoint))home_center
{
    return ^(CGPoint center){
        self.center = center;
        return self;
    };
}

-(UIButton *(^)(NSInteger))home_tag
{
    return ^(NSInteger tag){
        self.tag = tag;
        return self;
    };
}

-(UIButton *(^)(CGFloat))home_cornerRadius
{
    return ^(CGFloat cornerRadius){
        self.layer.cornerRadius = cornerRadius;
        return self;
    };
}

-(UIButton *(^)(__kindof UIView *))home_superView
{
    return ^(UIButton* superView){
        [superView addSubview:self];
        return self;
    };
}

-(UIButton *(^)(BOOL))home_hidden
{
    return ^(BOOL hidden){
        self.hidden = hidden;
        return self;
    };
}

-(UIButton *(^)(BOOL))home_clipsToBounds
{
    return ^(BOOL clipsToBounds){
        self.clipsToBounds = clipsToBounds;
        return self;
    };
}

-(UIButton *(^)(BOOL))home_userInteractionEnabled
{
    return ^(BOOL userInteractionEnabled){
        self.userInteractionEnabled = userInteractionEnabled;
        return self;
    };
}

-(UIButton *(^)(BOOL))home_translatesAutoresizingMaskIntoConstraints
{
    return ^(BOOL translatesAutoresizingMaskIntoConstraints){
        self.translatesAutoresizingMaskIntoConstraints = translatesAutoresizingMaskIntoConstraints;
        return self;
    };
}

-(UIButton *(^)(CGFloat))home_alpha
{
    return ^(CGFloat alpha){
        self.alpha = alpha;
        return self;
    };
}

-(UIButton* (^) (UIViewContentMode))home_contentMode
{
    return ^(UIViewContentMode contentMode){
        self.contentMode = contentMode;
        return self;
    };
}

-(UIButton* (^) (CGAffineTransform))home_transform
{
    return ^(CGAffineTransform transform){
        self.transform = transform;
        return self;
    };
}

-(UIButton* (^) (UISemanticContentAttribute))home_semanticContentAttribute
{
    return ^(UISemanticContentAttribute semanticContentAttribute){
        self.semanticContentAttribute = semanticContentAttribute;
        return self;
    };
}

-(UIButton* (^) (CGFloat))home_contentScaleFactor
{
    return ^(CGFloat contentScaleFactor){
        self.contentScaleFactor = contentScaleFactor;
        return self;
    };
}

-(UIButton* (^) (BOOL))home_multipleTouchEnabled
{
    return ^(BOOL multipleTouchEnabled){
        self.multipleTouchEnabled = multipleTouchEnabled;
        return self;
    };
}

-(UIButton* (^) (BOOL))home_exclusiveTouch
{
    return ^(BOOL exclusiveTouch){
        self.exclusiveTouch = exclusiveTouch;
        return self;
    };
}

-(UIButton* (^) (BOOL))home_autoresizesSubviews
{
    return ^(BOOL autoresizesSubviews){
        self.autoresizesSubviews = autoresizesSubviews;
        return self;
    };
}

-(UIButton* (^) (UIViewAutoresizing))home_autoresizingMask
{
    return ^(UIViewAutoresizing autoresizingMask){
        self.autoresizingMask = autoresizingMask;
        return self;
    };
}

-(UIButton* (^) (UIEdgeInsets))home_layoutMargins
{
    return ^(UIEdgeInsets layoutMargins){
        self.layoutMargins = layoutMargins;
        return self;
    };
}

-(UIButton* (^) (BOOL))home_preservesSuperviewLayoutMargins
{
    return ^(BOOL preservesSuperviewLayoutMargins){
        self.preservesSuperviewLayoutMargins = preservesSuperviewLayoutMargins;
        return self;
    };
}

-(UIButton* (^) (BOOL))home_opaque
{
    return ^(BOOL opaque){
        self.opaque = opaque;
        return self;
    };
}

-(UIButton* (^) (BOOL))home_clearsContextBeforeDrawing
{
    return ^(BOOL clearsContextBeforeDrawing){
        self.clearsContextBeforeDrawing = clearsContextBeforeDrawing;
        return self;
    };
}

-(UIButton* (^) (UIView *))home_maskView
{
    return ^(UIView *maskView){
        self.maskView = maskView;
        return self;
    };
}

-(UIButton* (^) (UIColor *))home_tintColor
{
    return ^(UIColor *tintColor){
        self.tintColor = tintColor;
        return self;
    };
}

-(UIButton* (^) (UIViewTintAdjustmentMode))home_tintAdjustmentMode
{
    return ^(UIViewTintAdjustmentMode tintAdjustmentMode){
        self.tintAdjustmentMode = tintAdjustmentMode;
        return self;
    };
}

-(UIButton* (^) (NSArray<__kindof UIGestureRecognizer *> *))home_gestureRecognizers
{
    return ^(NSArray<__kindof UIGestureRecognizer *> *gestureRecognizers){
        self.gestureRecognizers = [gestureRecognizers copy];
        return self;
    };
}

-(UIButton* (^) (NSArray<__kindof UIMotionEffect *> *))home_motionEffects
{
    return ^(NSArray<__kindof UIMotionEffect *> *motionEffects){
        self.motionEffects = [motionEffects copy];
        return self;
    };
}

-(UIButton* (^) (NSString *))home_restorationIdentifier
{
    return ^(NSString *restorationIdentifier){
        self.restorationIdentifier = restorationIdentifier;
        return self;
    };
}

#pragma mark - UIControl
-(UIButton* (^) (BOOL))home_enabled
{
    return ^(BOOL enabled){
        self.enabled = enabled;
        return self;
    };
}

-(UIButton* (^) (BOOL))home_selected
{
    return ^(BOOL selected){
        self.selected = selected;
        return self;
    };
}

-(UIButton* (^) (BOOL))home_highlighted
{
    return ^(BOOL highlighted){
        self.highlighted = highlighted;
        return self;
    };
}

-(UIButton* (^) (UIControlContentVerticalAlignment))home_contentVerticalAlignment
{
    return ^(UIControlContentVerticalAlignment contentVerticalAlignment){
        self.contentVerticalAlignment = contentVerticalAlignment;
        return self;
    };
}

-(UIButton* (^) (UIControlContentHorizontalAlignment))home_contentHorizontalAlignment
{
    return ^(UIControlContentHorizontalAlignment contentHorizontalAlignment){
        self.contentHorizontalAlignment = contentHorizontalAlignment;
        return self;
    };
}

#pragma mark - UIButton

-(UIButton * (^) (NSString *text))home_text{
    return ^(NSString *text){
        [self setTitle:text forState:UIControlStateNormal];
        return self;
    };
}

-(UIButton* (^) (UIEdgeInsets))home_contentEdgeInsets
{
    return ^(UIEdgeInsets contentEdgeInsets){
        self.contentEdgeInsets = contentEdgeInsets;
        return self;
    };
}

-(UIButton* (^) (UIEdgeInsets))home_titleEdgeInsets
{
    return ^(UIEdgeInsets titleEdgeInsets){
        self.titleEdgeInsets = titleEdgeInsets;
        return self;
    };
}

-(UIButton* (^) (BOOL))home_reversesTitleShadowWhenHighlighted
{
    return ^(BOOL reversesTitleShadowWhenHighlighted){
        self.reversesTitleShadowWhenHighlighted = reversesTitleShadowWhenHighlighted;
        return self;
    };
}

-(UIButton* (^) (UIEdgeInsets))home_imageEdgeInsets
{
    return ^(UIEdgeInsets imageEdgeInsets){
        self.imageEdgeInsets = imageEdgeInsets;
        return self;
    };
}

-(UIButton* (^) (BOOL))home_adjustsImageWhenHighlighted
{
    return ^(BOOL adjustsImageWhenHighlighted){
        self.adjustsImageWhenHighlighted = adjustsImageWhenHighlighted;
        return self;
    };
}

-(UIButton* (^) (BOOL))home_adjustsImageWhenDisabled
{
    return ^(BOOL adjustsImageWhenDisabled){
        self.adjustsImageWhenDisabled = adjustsImageWhenDisabled;
        return self;
    };
}

-(UIButton* (^) (BOOL))home_showsTouchWhenHighlighted
{
    return ^(BOOL showsTouchWhenHighlighted){
        self.showsTouchWhenHighlighted = showsTouchWhenHighlighted;
        return self;
    };
}
-(UIButton* (^) (ActionBlock block))home_block{
    return ^(ActionBlock block){
        objc_setAssociatedObject(self, overviewKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
        [self addTarget:self action:@selector(callActionBlock:) forControlEvents:UIControlEventTouchUpInside];
        return self;
    };
}
- (void)callActionBlock:(id)sender {
    ActionBlock block = (ActionBlock)objc_getAssociatedObject(self, overviewKey);
    if (block) {
        block(sender);
    }
}
@end
